<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <el-card shadow="never">
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :columns="columns"
        :datasource="datasource"
        :default-expand-all="false"
        :need-page="false"
        :parse-data="parseData"
        @done="onDone" :height="tableHeight" :full-height="tableFullHeight" >
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <!-- <el-button
            size="small"
            icon="el-icon-upload2"
            class="ele-btn-icon"
            @click="reportexcel"
          >
            导出
          </el-button> -->
          <el-button class="ele-btn-icon" size="small" @click="expandAll">
            展开全部
          </el-button>
          <el-button class="ele-btn-icon" size="small" @click="foldAll">
            折叠全部
          </el-button>
        </template>
        <!-- 标题列 -->
        <template slot="title" slot-scope="{ row }">
          <i :class="row.icon"></i> {{ row.title }}
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{ row }">
          <el-link
          v-permission="'Basic_Org_Add'" 
            v-show="row.type<10"
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            @click="openEdit(row,1)"
          >
            新增
          </el-link>
          <el-link
          v-permission="'Basic_Org_Update'" 
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row,2)"
          >
            修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要作废吗？"
            @confirm="remove(row)"
          >
            <el-link
            v-permission="'Basic_Org_Deleted'" 
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-show="row.oLevel!=1"
            >
            作废
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>
    </el-card>
   
    <!-- 编辑部门弹窗 -->
    <OrganizationEdit :data="current" :visible.sync="showEdit" @done="reload" />
    <!-- 导出数据 -->
    <ExportDialog ref="ExportDialog"></ExportDialog>
  </div>
</template>

<script>
  import OrganizationEdit from './components/organization-edit.vue';
  import ExportDialog from '@/components/Excel/ExportDialog'
  // import { QueryOrganiztionTypeEnum } from '@/api/enummanger';
  import { QueryTreeOrganiztion,DeleteOrganiztion,QueryCurrentOrganiztion } from '@/api/usermanager/organizationmanager';
  export default {
    name: 'Organization',
    components: {ExportDialog,OrganizationEdit,},
    data() {
      return {
        // 表格列配置
        columns: [
          {
            prop: 'name',
            label: '名称',
            showOverflowTooltip: true,
          },
          {
            prop: 'code',
            label: '代码',
            showOverflowTooltip: true,
          },
          {
            prop: 'oLevel',
            label: '层级',
            showOverflowTooltip: true,
          },
          {
            prop: 'typeDesc',
            label: '分类',
            showOverflowTooltip: true,
          },
          // {
          //   prop: 'statusDesc',
          //   label: '状态',
          //   showOverflowTooltip: true,
          // },
          {
            columnKey: 'action',
            label: '操作',
            width: 300,
            align: 'center',
            resizable: false,
            slot: 'action',
            hideInSetting: true,
            showOverflowTooltip: true
          }
        ],
        fixedHeight:true,
        // 当前编辑数据
        current: null,
         // 是否显示新增组织编辑弹窗
        showEdit:false,
        // 全部菜单数据
        menuList: [],
        // //组织类型
        // orgTypeNums:[],
        // 导出列
        exportList: [
          { name: "名称", keys: "name", status: true },
          { name: "代码", keys: "code", status: true },
          { name: "层级", keys: "oLevel", status: true },
          { name: "分类", keys: "typeDesc", status: true },
          // { name: "状态", keys: "statusDesc", status: true },
        ]
      };
    },
    computed: {
      // 表格高度
      tableHeight() {
        var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

        return fh;
      },
      // 表格全屏时高度
      tableFullHeight() {
        var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

        return fh;
      },
    },
    methods: {
      /* 初始化数据 */
      async initData(){
        let orgNums = await QueryOrganiztionTypeEnum();
        //菜单类型
        orgNums.map(m=>{
            this.orgTypeNums.push({Key:m.Key,Value:m.Value});
        });
      },

      /* 表格数据源 */
      datasource() {
        this.$nextTick(() => {
          this.$refs.table.doLayout();
        });
        var data = QueryTreeOrganiztion();

        return data;
      },
      /* 数据转为树形结构 */
      parseData(data) {

        let treeData = this.$util.toTreeData({
          data: data,
          idField: 'id',
          parentIdField: 'parentId'
        });
        return treeData;
      },
      /* 表格渲染完成回调 */
      onDone({ data }) {
        this.menuList = data;
      },
      /* 刷新表格 */
      reload() {
        this.$refs.table.reload();
      },
      
      /**
     * 新增部门显示编辑
     * @param row 当前行对像
     */
      openEdit(row,index) {
        this.current = null;
        if(index==1){
          this.current = {};
          this.showEdit = true;
          this.current.parentId=row.id;
          this.current.parentName=row.name;
          if(row.oLevel)this.current.oLevel= parseInt(row.oLevel)+1 ;
        }else{
          this.current = row;
          this.showEdit = true;
          this.current.parentId=row.parentId;
          this.current.parentName=row.parentName;
        }
      },

      /* 删除 */
      remove(row) {
        if (row.hasChild) {
          this.$message.error('请先删除子节点');
          return;
        }
        this.loading = true;
        DeleteOrganiztion(row.id)
          .then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      },

      /* 导出 */
      async reportexcel(pams) {
        let datas = await QueryCurrentOrganiztion();

        this.$refs.ExportDialog.showEdits = true
        this.$refs.ExportDialog.checkList = this.exportList
        this.$refs.ExportDialog.titleName = "菜单列表"
        this.$refs.ExportDialog.dataList = datas;
      },
      /* 展开全部 */
      expandAll() {
        this.$refs.table.toggleRowExpansionAll(true);
      },
      /* 折叠全部 */
      foldAll() {
        this.$refs.table.toggleRowExpansionAll(false);
      },
    }
  };
</script>
<style lang="scss" scoped>
/*总计字体颜色*/
  ::v-deep .el-table__footer-wrapper tbody td {
    color: var(--color-primary);
    cursor: pointer;
  }

  // ::v-deep .el-table__body-wrapper {
  //   height: 600px !important;
  //   overflow-y:auto !important;
  // }
</style>
